import { connect, play } from "./networking";
import "./css/normalize.css";
import "./css/main.css";
import { $ } from "./util";
import { downloadAssets } from "./asset";
import { stopRendering, startRendering } from "./render";
import { stopCapturingInput, startCapturingInput } from "./input";

Promise.all([connect(gameOver), downloadAssets()])
  .then(() => {
    //隐藏连接服务器,显示输入框及按钮
    $(".connect").classList.add("hidden");
    $(".play").classList.remove("hidden");

    // 默认聚焦输入框
    $("#home input").focus();

    // 游戏开始按钮监听点击事件
    $("#play-button").onclick = () => {
      // 判断输入框的值是否为空
      let val = $("#home input").value;
      if (val.replace(/\s*/g, "") === "") {
        alert("名称不能为空！");
        return;
      }

      // 游戏开始，隐藏开始界面
      $("#home").classList.add("hidden");
      play(val);
      startRendering();
      startCapturingInput();
    };
  })
  .catch((err) => console.error(err));

function gameOver() {
  stopRendering();
  stopCapturingInput();
  $("#home").classList.remove("hidden");
  alert("game over");
}
